<template>
  <el-container class="home-container">
    <el-header>
      <span>电商后台管理系统</span>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggle">|||</div>
       <el-menu
      class="el-menu-vertical-demo"
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409eff"
      :unique-opened="true"
      :collapse="isCollapse"
      :default-active="activePath"
      :collapse-transition="false"
      :router="true"
      >
      <el-submenu :index="item.id+``" v-for="item in menuList" :key="item.id">
        <template slot="title">
          <i :class="iconObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item v-for="child in item.children" :key="child.id" :index="'/'+child.path" @click="saveNavState('/'+child.path)">
          <template slot="title">
            <i class="el-icon-menu"></i>
            <span>{{child.authName}}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
//清空token
//跳转至登录页
export default {
  created() {
    this.activePath=window.sessionStorage.getItem("activePath")
  },
  data(){
    return{
      menuList:[
        {
          "id": 125,
          "authName": "用户管理",
          "path": "users",
          "children": [
            {
              "id": 110,
              "authName": "用户列表",
              "path": "users",
              "children": [],
              "order": null
            }
          ],
          "order": 1
        },
        // {
        //   "id": 103,
        //   "authName": "权限管理",
        //   "path": "rights",
        //   "children": [
        //     {
        //       "id": 111,
        //       "authName": "角色列表",
        //       "path": "roles",
        //       "children": [],
        //       "order": null
        //     },
        //     {
        //       "id": 112,
        //       "authName": "权限列表",
        //       "path": "rights",
        //       "children": [],
        //       "order": null
        //     }
        //   ],
        //   "order": 2
        // },
        {
          "id": 101,
          "authName": "商品管理",
          "path": "goods",
          "children": [
            {
              "id": 104,
              "authName": "商品列表",
              "path": "goods",
              "children": [],
              "order": 1
            }
          ],
          "order": 3
        },
        {
          "id": 102,
          "authName": "订单管理",
          "path": "orders",
          "children": [
            {
              "id": 107,
              "authName": "订单列表",
              "path": "orders",
              "children": [],
              "order": null
            }
          ],
          "order": 4
        },
        {
          "id": 145,
          "authName": "日志信息",
          "path": "reports",
          "children": [
            {
              "id": 146,
              "authName": "日志报表",
              "path": "reports",
              "children": [],
              "order": null
            }
          ],
          "order": 5
        }
      ],
      iconObj:{
      '125':'el-icon-user',
      '103':'el-icon-s-goods',
      '102':'el-icon-coordinate',
      '101':'el-icon-document',
      '145':'el-icon-document'},
      isCollapse:false,
      activePath:''
    }
  },
  methods: {
    logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    getMenuList:async function(){
      const {data:res} = await this.$http.get('menus');
      console.log(res);
      if(res.meta.status!==200){
        return this.$message.error(res.meta.msg);
      }
      this.menuList=res.data;
      console.log(this.menuList);
    },
    toggle:function(){
      this.isCollapse=!this.isCollapse;
    },
    // 保存链接激活状态
    saveNavState:function(activePath){
      window.sessionStorage.setItem("activePath",activePath)
      this.activePath=activePath
    }
  },
}
</script>
<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
}
.el-aside{
  background-color: #333744;
}
.el-main{
  background-color: #eaedf1;
}
i{
  margin-right: 10px;
}
.el-menu{
  border-right: none;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
